﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataShow.scene
{
    public partial class Rain : System.Web.UI.Page
    {
        #region 页面加载事件
        private bool isByHand;//是否是手工站
        protected string URL;
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            bool indexURL = Convert.ToBoolean(Session["isadmin"]);
            if (indexURL)
            {
                URL = "/Index.aspx";
            }
            else
            {
                URL = "/ForecasterIndex.aspx";
            }
            string parameter = Request.QueryString["isbyhand"];
            if (string.IsNullOrEmpty(parameter))
            {
                isByHand = false;
            }
            else
            {
                isByHand = true;
            }
            this.anp.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"]);
            if (!IsPostBack)
            {
                time_begin.Text = DateTime.Now.AddDays(0).ToString("yyyy-MM-dd");
                time_end.Text = DateTime.Now.ToString("yyyy-MM-dd");
                Common.CheckAllCheckbox(cbl_types); cbx_type_all.Checked = true;
                Common.CheckAllCheckbox(cbl_sites); cbx_station_all.Checked = true;
                Search();
            }
        }
        #endregion



        #region 搜索按钮事件
        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_search_Click(object sender, EventArgs e)
        {
            Search();
        }
        #endregion

        #region 搜索结果并绑定
        /// <summary>
        /// 搜索结果并绑定
        /// </summary>
        public void Search(int index = 1)
        {
            DateTime Time_Begin = DateTime.Parse(time_begin.Text.Trim());
            DateTime Time_End = DateTime.Parse(time_end.Text.Trim());
            List<Types> Types = new List<Types>();
            foreach (ListItem item in cbl_types.Items)
            {
                if (item.Selected)
                {
                    Types.Add((Types)Enum.Parse(typeof(Types), item.Value));
                }
            }
            List<Station> Stations = new List<Station>();
            foreach (ListItem item in cbl_sites.Items)
            {
                if (item.Selected)
                {
                    Stations.Add((Station)Enum.Parse(typeof(Station), item.Value));
                }
            }

            if (Stations.Count == 0)
            {
                anp.Visible = false;
                gv_result.Visible = false;
                div_nodata.Visible = true;
                return;
            }
            anp.CurrentPageIndex = index;
            int pagesize = anp.PageSize;
            int pageindex = anp.CurrentPageIndex;

            var lists = new codeBLL.SceneBLL().SearchRain(Time_Begin, Time_End, Types, Stations, pagesize/Stations.Count, pageindex, isByHand);
            gv_result.DataSource = lists;
            anp.RecordCount = new codeBLL.SceneBLL().ClaculateRainCount(Time_Begin, Time_End, Types, isByHand, Stations)*Stations.Count;
            gv_result.DataBind();
            if (anp.RecordCount == 0)
            {
                gv_result.Visible = false;
                anp.Visible = false;
                div_nodata.Visible = true;
            }
            else
            {
                gv_result.Visible = true;
                anp.Visible = true;
                div_nodata.Visible = false;
            }
        }
        #endregion

        #region 全选类型
        /// <summary>
        /// 全选类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbx_type_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_types.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }
        #endregion

        #region 全选站点
        /// <summary>
        /// 全选站点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbx_station_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_sites.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }
        #endregion

        #region 选择站点改变事件
        /// <summary>
        /// 选择站点改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbl_sites_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sites.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_station_all.Checked = false;
            }
        }
        #endregion


        #region 选择类型改变事件
        /// <summary>
        /// 选择类型改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbl_types_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sites.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_type_all.Checked = false;
            }
            anp.CurrentPageIndex = 1;
        }
        #endregion

        #region 翻页事件
        /// <summary>
        /// 翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void anp_PageChanged(object sender, EventArgs e)
        {
            
            Search(anp.CurrentPageIndex);
        }
        #endregion

        


    }
}